package com.cs157a.view;

import java.util.List;

import javax.swing.table.AbstractTableModel;

import com.cs157a.dao.PartType;
import com.cs157a.dao.RdiPart;

/**
 * This class is the table model used for displaying part information.
 * 
 * @author Hiep
 * 
 */
public class PartTableModel extends AbstractTableModel {
	private static final long serialVersionUID = 20012100301L;

	private List<RdiPart> parts;

	public void setParts(List<RdiPart> parts) {
		this.parts = parts;
		fireTableDataChanged();
	}
	
	public void removePart( int index ) {
		parts.remove(index);
		fireTableDataChanged();
	}

	public PartTableModel(List<RdiPart> parts) {
		this.parts = parts;
	}

	@Override
	public String getColumnName(int columnIndex) {
		PartColumn partColumn = PartColumn.values()[columnIndex];
		return partColumn.getName();
	}

	@Override
	public int getRowCount() {
		return parts.size();
	}

	@Override
	public int getColumnCount() {
		return PartColumn.values().length;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Object value = null;
		switch (PartColumn.values()[columnIndex]) {
		case VENDOR:
			value = parts.get(rowIndex).getVendor();
			break;
		case PART_NUMBER:
			value = parts.get(rowIndex).getPartNumber();
			break;
		case CORE:
			value = parts.get(rowIndex).getCore();
			break;
		case IN_HEAD:
			value = parts.get(rowIndex).getInHead();
			break;
		case OUT_HEAD:
			value = parts.get(rowIndex).getOutHead();
			break;
		case IN_CON:
			value = parts.get(rowIndex).getInCon();
			break;
		case OUT_CON:
			value = parts.get(rowIndex).getOuCon();
			break;
		case T_MOUNT:
			value = parts.get(rowIndex).getTMount();
			break;
		case OIL_COOL:
			value = parts.get(rowIndex).getOilCool();
			break;
		case PRICE:
			value = parts.get(rowIndex).getPrice();
			break;
		case AMOUNT:
			value = parts.get(rowIndex).getAmount();
			break;
		case TYPE:
			value = parts.get(rowIndex).getType();
		}

		return value;
	}

	public RdiPart getVAlueAt(int rowIndex) {
		return parts.get(rowIndex);
	}

}
